package com.sh.data.engine.domain.base.model;

import cn.hutool.extra.spring.SpringUtil;
import com.aliyun.gts.sso.context.SFLoginAccountContext;
import com.aliyun.gts.sso.domain.LoginInfo;
import com.aliyun.gts.sso.domain.User4ADTO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sh.data.engine.domain.workspace.manager.model.domain.UserDomain;
import com.sh.data.engine.domain.workspace.manager.service.impl.UserServiceImpl;
import com.sh.data.engine.repository.dao.authority.UserTokenMapper;
import com.sh.data.engine.repository.dao.authority.entity.UserTokenEntity;

import static com.sh.data.engine.common.constants.Constants.OPEN_API;
import static com.sh.data.engine.common.constants.Constants.WEB_API;

/**
 * @author: mengzheng.mhc
 * @date: 2024/6/20 18:17
 */
public class UserContext {

    private static final ThreadLocal<UserDomain> ACCOUNT_THREAD_LOCAL = new ThreadLocal<>();

    private static UserServiceImpl userService = SpringUtil.getBean(UserServiceImpl.class);
    private static UserTokenMapper userTokenMapper = SpringUtil.getBean(UserTokenMapper.class);

    public static void putToken(UserDomain tokenVo) {
        ACCOUNT_THREAD_LOCAL.set(tokenVo);
    }

//    public static String getUserId() {
//        LoginInfo loginInfo = SFLoginAccountContext.getLoginInfo();
//        if (loginInfo == null) {
//            return null;
//        }
//        LoginAccount loginAccount = loginInfo.getLoginAccount();
//        if (loginAccount == null) {
//            return null;
//        }
//        return loginAccount.getGuid();
//    }

    public static String getUserId() {
        String status = String.valueOf(SFLoginAccountContext.getStatus());
        if ("200".equals(status)) {
            LoginInfo loginInfo = (LoginInfo) SFLoginAccountContext.getLoginInfo();
            if (loginInfo != null) {
                return loginInfo.getLoginAccount().getGuid();
            }
        } else if ("201".equals(status)) {
            User4ADTO user = (User4ADTO) SFLoginAccountContext.getLoginInfo();
            if (user != null) {
                return user.getId();
            }
        }
        return null;
    }

    public static String getApiType() {
        Integer status = SFLoginAccountContext.getStatus();
        if (status == 200) {
            return WEB_API;
        } else if (status == 201) {
            return OPEN_API;
        }
        return null;
    }

    public static UserDomain getUserInfo() {
        String userId = getUserId();
        if (userId == null) {
            return null;
        }
        UserDomain user = userService.getUserById(userId);
        user.setUserToken(extractUserToken(userId));
        return user;
    }

    public static UserDomain getUserInfoWithoutRole() {
        String userId = getUserId();
        if (userId == null) {
            return null;
        }
        UserDomain user = userService.getUserDetailByIdWithoutRole(userId);
        user.setUserToken(extractUserToken(userId));
        return user;
    }

    public static String extractUserToken(String userId) {
        LambdaQueryWrapper<UserTokenEntity> wrapperToken = new LambdaQueryWrapper<>();
        wrapperToken.eq(UserTokenEntity::getUserId, userId);
        return userTokenMapper.selectList(wrapperToken).get(0).getUserToken();
    }

    public static void destory() {
        ACCOUNT_THREAD_LOCAL.remove();
    }
}